<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

/**
 * This function handles the php portion of clearing the file picker
 *
 * @package   theme-rlmaster
 * @copyright 2011 Remote Learner  http://www.remote-learner.net/
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

require_once('../../config.php');

// Check permissions
require_login(0, false);

$picker  = required_param('picker',  PARAM_INT);
$setting = required_param('setting', PARAM_ALPHANUMEXT);
$theme   = basename(dirname(__FILE__));

$usercontext = context_user::instance($USER->id);
$fs          = get_file_storage();

$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $picker);

foreach ($files as $file) {
    $file->delete();
}

// We don't delete the old file because it could be used elsewhere in the theme

set_config($setting, '', 'theme_'. $theme);